//
//  SearchViewController.m
//  Item A House
//
//  Created by dlios on 15/5/28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SearchViewController.h"
#import "SANetWorkingTask.h"
#import "RankModel.h"
#import "RankTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "MainJumpViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

@end

@implementation SearchViewController
- (void)dealloc
{
    [_searchArray release];
    [_searchBar release];
    [_searchTable release];
    [_searchText release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.searchArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSearchBar];
    [self createTable];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(15, 5, 38, 38);
    
    [btn setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    
    [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    
    UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=back;
}
-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 创建searchBar
- (void)createSearchBar
{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    self.searchBar.placeholder = @"搜索";
    self.searchBar.delegate = self;
    self.searchBar.autocapitalizationType = UITextAutocorrectionTypeYes;
    self.searchBar.showsCancelButton = NO;
    self.navigationItem.titleView = self.searchBar;
    [self.searchBar release];
    
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchText = self.searchBar.text;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self createConnection];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.searchBar resignFirstResponder];
}
- (void)createConnection
{
    
    NSDictionary *dic = @{@"city_id":@"21", @"page":@"1", @"page_size":@"10", @"keywords":self.searchText};
    [SANetWorkingTask request:@"http://api.anjuke.com/xinfang/m/iphone/1.3/loupan/newlist/" parmater:dic block:^(NSDictionary *result) {
        NSDictionary *bigDic = [result objectForKey:@"result"];
        NSMutableArray *arr = [bigDic objectForKey:@"rows"];
        self.searchArray = [NSMutableArray array];
        for (NSDictionary *smallDic in arr) {
            RankModel *model = [[RankModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.searchArray addObject:model];
            [model release];
        }
        
        [self.searchTable reloadData];
    }];
}
- (void)createTable
{
    self.searchTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT) style:UITableViewStylePlain];
    self.searchTable.rowHeight = 100;
    [self.searchTable registerClass:[RankTableViewCell class] forCellReuseIdentifier:@"ranktable"];
    self.searchTable.delegate = self;
    self.searchTable.dataSource = self;
    [self.view addSubview:self.searchTable];
    [self.searchTable release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.searchArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RankTableViewCell *rank = [tableView dequeueReusableCellWithIdentifier:@"ranktable"];
    RankModel *model = [self.searchArray objectAtIndex:indexPath.row];
    [rank.image sd_setImageWithURL:[NSURL URLWithString:model.default_image] placeholderImage:[UIImage imageNamed:@"10.jpg"]];;
    rank.loupan.text = model.loupan_name;
    rank.region.text = [NSString stringWithFormat:@"%@  %@", model.region_title, model.sub_region_title];
    rank.tags.text = model.tags;
    rank.price.text = [NSString stringWithFormat:@"%ld元/平", (long)model.price];
    return rank;
}
//跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MainJumpViewController *jump = [[MainJumpViewController alloc] init];
    RankModel *model = [self.searchArray objectAtIndex:indexPath.row];
    jump.loupan_id = model.loupan_id;
    jump.loupan_name = model.loupan_name;
    jump.rankModel = [self.searchArray objectAtIndex:indexPath.row];
    jump.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:jump animated:YES];
    [jump release];
    
    
}
@end
